<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商管理后台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <!-- 折叠 -->
        <div class="toggle-button" @click="toggleButton">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409Eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="currentActive"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + '' " :key="item.id" v-for="item in menuList">
            <!-- index只接收字符串，但是item.id是数字 拼接一个空的字符串 -->
            <template slot="title">
              <i :class="iconfontObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单a -->
            <el-menu-item
              :index=" '/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavSates('/' + subItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span slot="title">{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      isCollapse: false,
      // 自定义图标
      iconfontObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      // 链接的激活状态
      currentActive: ''
    };
  },
  // 页面刚一加载的时候，就应该获取列表数据
  created() {
    this.getMenuList();
    this.currentActive = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      //  将获取到的数据挂载到自己的data属性上
    },
    // 折叠
    toggleButton() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存链接状态
    saveNavSates(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.currentActive = activePath
    }
  }
};
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #333d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background: #333d41;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background: #eeee;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background: #4a5064;
  font-size: 10px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  cursor: pointer;
}
</style>